home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / smalltlk.zip / SOURCES / LINE.C < prev    next >
Text File  |  1990-09-12  |  2KB  |  100 lines

  1. /*
  2.     Little Smalltalk
  3.  
  4.         line grabber - does lowest level input for command lines.
  5. */
  6. /*
  7.     The source code for the Little Smalltalk System may be freely
  8.     copied provided that the source of all files is acknowledged
  9.     and that this condition is copied with each file.
  10.  
  11.     The Little Smalltalk System is distributed without responsibility
  12.     for the performance of the program and without any guarantee of
  13.     maintenance.
  14.  
  15.     All questions concerning Little Smalltalk should be addressed to:
  16.  
  17.         Professor Tim Budd
  18.         Department of Computer Science
  19.         Oregon State University
  20.         Corvallis, Oregon
  21.         97331
  22.         USA
  23. */
  24. # include <stdio.h>
  25. # include "object.h"
  26. # include "primitiv.h"
  27.  
  28. # define MAXINCLUDE  10
  29. # define MAXBUFFER  1200        /* text buffer */
  30.  
  31. static FILE *fdstack[MAXINCLUDE];
  32. static int fdtop = -1;
  33.  
  34. static char buffer[MAXBUFFER];
  35. static char *buftop = buffer;
  36. char *lexptr = buffer;
  37. static enum {empty, half, filled} bufstate = empty;
  38. int inisstd = 0;
  39. extern object *o_tab;
  40.  
  41. /* set file - set a file on the file descriptor stack */
  42. set_file(fd)
  43. FILE *fd;
  44. {
  45.     if ((++fdtop) > MAXINCLUDE)
  46.         cant_happen(18);
  47.     fdstack[fdtop] = fd;
  48.     if (fd == stdin) inisstd = 1;
  49.     else inisstd = 0;
  50. }
  51.  
  52. /* line-grabber - read a line of text
  53.     do blocked i/o if blocked is nonzero,
  54.     otherwise do non-blocking i/o */
  55.  
  56. int line_grabber(block)
  57. int block;
  58. {
  59.     /* if it was filled last time, it is now empty */
  60.     if (bufstate == filled) {
  61.         bufstate = empty;
  62.         buftop = buffer;
  63.         lexptr = buffer;
  64.         }
  65.  
  66.     if ( ! block)
  67.         return(0); /* for now, only respond to blocked requests*/
  68.     else while (bufstate != filled) {
  69.         if (fdtop < 0) {
  70.             fprintf(stderr,"no files to read\n");
  71.             return(-1);
  72.             }
  73.         if (inisstd && o_tab)
  74.             primitive(RAWPRINT, 1, &o_tab);
  75.         if (fgets(buftop, MAXBUFFER, fdstack[fdtop]) == NULL) {
  76.             bufstate = empty;
  77.             if (fdstack[fdtop] != stdin)
  78.                 fclose(fdstack[fdtop]);
  79.             if (--fdtop < 0) return(-1);
  80.             inisstd = (fdstack[fdtop] == stdin);
  81.             }
  82.         else {
  83.             bufstate = half;
  84.             while (*buftop) buftop++;
  85.             if (*(buftop-1) == '\n') {
  86.                 if (*(buftop-2) == '\\') {
  87.                     buftop -= 2;
  88.                     }
  89.                 else {
  90.                     if ((buftop - buffer) > MAXBUFFER)
  91.                         cant_happen(18);
  92.                     *buftop = '\0';
  93.                     bufstate = filled;
  94.                     }
  95.                 }
  96.             }    
  97.         }
  98.     return(bufstate == filled);
  99. }
  100.